<?php
include_once "function_xml_basic.php";

$xpath_root = "/MINI/DEBUG/REMOTEDEBUG";
$xpath_node = "/REMOTEDEBUG";

include_once "function_base_xml_class.php";
class CRemoteDebug
{
	private $root_path = "/MINI/DEBUG";
	private $node_name= "REMOTEDEBUG";
	private $key_array = array("AutoStart");

	function GetXmlItemByNumber($num) // From zero start !!!
	{
		$s = new base_xml_class($this->root_path, $this->node_name, $this->key_array);
		return $s->get_item_by_num($num);
	}
	function EditItemByNumber($num, $in_array)
	{
		$s = new base_xml_class($this->root_path, $this->node_name, $this->key_array);
		$s->edit_item_by_num($num, $in_array);
	}
	function Apply() // Only for system boot.
	{
		$num = 0;
		$type = $this->GetXmlItemByNumber($num);
		if ($type['AutoStart'] == "1")	// auto start
			ApplySysDebugRemote("RUN");
		
		return TRUE;
	}
}

function ApplySysReboot()
{
	$command = "/bin/sleep 3 && /sbin/reboot &";

	$ret_string = shell_exec($command);
	return $ret_string;
}

function GetAttrList()
{
	$returns = array( "Enabled", "AutoStart");
	return $returns;
}

function GetXmlRemoteDebugList()
{
	global $xpath_root;
	global $xpath_node;

	$query_string = $xpath_root. $xpath_node;
	$key_array = GetAttrList();
	
	$list = GetAttributList($query_string, $key_array);
	$ret_node = $list[0];
	#for ($i = 0; $i < count($list); $i ++)
	#{
	#	foreach ( $key_array as $key)
	#		print $i. $key. " = ". $list[$i][$key]. "\n";
	#}
	return $ret_node;
}
#$ret = GetXmlRemoteDebugList();
#print $ret["Enabled"];
#print $ret["AutoStart"];

function SetXmlRemoteDebugByNumber($number, $in_array )
{
	global $xpath_root;
	global $xpath_node;

	$query_string = $xpath_root. $xpath_node. "[". $number. "]";
	EditAllAttrOfNode($query_string, $in_array);
}
#$l["Enabled"] = "1";
#$l["AutoStart"] = "1";
#SetXmlRemoteDebugByNumber(1, $l);

function GetSysDebugRemote()
{
	$sshd_pid = "cat /var/run/sshd.pid.remote_debug 1>/dev/null 2>&1";
	$lsof_command = "/usr/sbin/lsof -i :65530 | sed -n '2p' |awk '{printf $2}'";
	
	$rid = shell_exec($lsof_command);

	if ($rid == '')
	{
		return FALSE;	// close
	}
	else	
	{
		return TRUE;	// running
	}	
}

function ApplySysDebugRemote($status)
{
	$sshd_command = "/usr/local/sbin/sshd -f /usr/local/etc/sshd_config.remote_debug 1>/dev/null 2>&1";
	$lsof_command = "/usr/sbin/lsof -i :65530 | sed -n '2p' |awk '{printf $2}'";
	$kill_command = "/bin/kill -9 ";
	$ipt = "/usr/local/bin/iptables ";

	$flush = " -F DEBUG";
	$table = " -A DEBUG ";
	$access = " -p tcp --dport 65530 -j ACCEPT";
	$iptable_command_f = $ipt. $flush;
	$iptable_command_a = $ipt. $table. $access;

	//print $iptable_command_f;
	shell_exec($iptable_command_f);
	$ret = shell_exec($lsof_command);
	$command = $kill_command. $ret. ' 1>/dev/null 2>&1';
	//print $command;
	shell_exec($command);

	if ($status == 'RUN')
	{
		//print $sshd_command;
		//print $iptable_command_a;
		shell_exec($iptable_command_a);
		$ret = shell_exec($sshd_command);
	}
	return TRUE;
}



function ApplyXmlForRemoteDebug()
{
	$command = "";
	$ready_command = "";
	
	$ret = shell_exec($ready_command);

	$ret_list = GetXmlRemoteDebugList();
	if ($ret_list["Enabled"] == "1"){
		$command = "";
		$ret = shell_exec($command);
	}

	return $ret;
}

function ApplyXmlForRemoteDebugOnBoot()
{
	$command = "";
	$ret_list = GetXmlRemoteDebugList();
	if ($ret_list["AutoStart"] == "1" && $ret_list["Enabled"] == "1")
		$ret = shell_exec($command);

	return $ret ;
}

?>
